/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.dao.impl.KamarDAOImpl;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Kos;
import com.ivanbiz.service.JTextFieldLimit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class KamarUpdatePanel extends javax.swing.JPanel {

    JDialog jDialog;
    Kamar kamar;
    Kos kos;

    public KamarUpdatePanel(JDialog jDialog) {
        initComponents();
        this.jDialog = jDialog;
    }

    public KamarUpdatePanel(JDialog jDialog, Kamar kamar) {
        initComponents();
        this.jDialog = jDialog;
        this.kamar = kamar;
        textKos.setText(kamar.getKos().getKodeKos());
        textKamar.setText(kamar.getKodeKamar());
//        textTarif.setText(String.valueOf(new Double(kamar.getTarif()).intValue()));
        this.kos = kamar.getKos();
        labelKamar.setText("Ubah Kamar");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelKamar = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        textKos = new javax.swing.JTextField();
        buttonCariKos = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        textKamar = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        labelKamar.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelKamar.setText("Tambah Kamar Baru");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel8.setText("Kos :");

        textKos.setEditable(false);

        buttonCariKos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonCariKos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariKosActionPerformed(evt);
            }
        });

        jLabel1.setText("No Kamar :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(textKos, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonCariKos))
                            .addComponent(textKamar, javax.swing.GroupLayout.Alignment.LEADING))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKos, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCariKos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textKamar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelKamar)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelKamar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        jDialog.dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (kamar == null) {
            kamar = new Kamar();
        }
        kamar.setKos(kos);
        kamar.setKodeKamar(textKamar.getText());
        kamar.setTarif(0);
        validate(kamar);
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonCariKosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariKosActionPerformed
        JDialog jDialogKos = new JDialog();
        jDialogKos.setModal(true);
        jDialogKos.setSize(800, 600);
        KosPanel kosPanel = new KosPanel(jDialogKos, "kamar");
        jDialogKos.add(kosPanel);
        jDialogKos.setLocationRelativeTo(null);
        jDialogKos.setVisible(true);
        this.kos = kosPanel.getKosKamar();
        textKos.setText(kosPanel.getKosKamar() == null ? null : kosPanel.getKosKamar().getKodeKos() + " - " + kosPanel.getKosKamar().getNama());
    }//GEN-LAST:event_buttonCariKosActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonCariKos;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelKamar;
    private javax.swing.JTextField textKamar;
    private javax.swing.JTextField textKos;
    // End of variables declaration//GEN-END:variables

    private void validate(Kamar kamar) {
        if (kamar.getKos() == null) {
            JOptionPane.showMessageDialog(this, "Kos tidak boleh kosong");
        } else if (kamar.getKodeKamar() == null) {
            JOptionPane.showMessageDialog(this, "No Kamar tidak boleh kosong");
        } else {
            try {
                KamarDAO kamarDAO = new KamarDAOImpl();
                if (kamarDAO.validateField(Kamar.class, "kodeKamar", kamar.getKodeKamar()).equalsIgnoreCase("Data sudah ada")) {
                    JOptionPane.showMessageDialog(this, "No Kamar sudah ada");
                } else {
                    kamarDAO.saveKamar(kamar);
                    jDialog.dispose();
                }
            } catch (Exception ex) {
                Logger.getLogger(KamarUpdatePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
